package business

import (
	"ginBasic/app"
	"ginBasic/app/common/model"
)

var Config = new(ConfigBus)

type ConfigBus struct {
	BaseBus
}

func (ConfigBus) GetList() (map[string]interface{}, error) {
	config, err := model.ConfigModel.List()
	if err != nil {
		return nil, err
	}
	configList := make(map[string]interface{})
	for _, v := range config {
		configList[v.Name] = v.Data
	}
	return configList, nil
}

func (ConfigBus) Update(req map[string]interface{}) error {
	column := model.ConfigModel.GetColumn()
	for key, val := range req {
		if app.IsValueInList(key, column) {
			if err := model.ConfigModel.Update(key, val); err != nil {
				return err
			}
		} else {
			if err := model.ConfigModel.Create(key, val); err != nil {
				return err
			}
		}
	}
	return nil
}
